package MydbQA::Command::Indexes;

use strict;
use warnings;

use base 'MydbQA::Command::Base';

use Carp;
use Getopt::Long;
use Storable qw (dclone);
use Time::HiRes qw( gettimeofday tv_interval );

use SQL::Builder::UseIndex;
use SQL::Builder::GroupBy;

use MySQL::Query::Performance;
use MySQL::Query::Performance::Count;
use MySQL::Query::Performance::GroupBy;
use MySQL::Query::IndexesOrder;

use HTML::Template;
use List::Util qw(min max sum);

sub run {
      my $self = shift;
      
      #get all indexes data
      $self->indexes_data;
      
      #do we have --order?
      if ($self->{order}) {
            my @tables_order = split /,/ , $self->{order};
      } 
      
      #get all indexes seq.
      $self->indexes_seq;
      
      
      $self->{html} ? $self->as_html : $self->as_text;
}

sub indexes_data {
      my ($self) = @_;
      
      $self->{_data}->{indexes} = [];
      while (my $table = $self->tables->next) {
            my %temp = ();
            $temp{indexes_t} = $table;
            $temp{indexes_t_read} = $self->indexes->read($table);
            push @{$self->{_data}->{indexes}} , \%temp;
      }
}

sub indexes_seq {
      my ($self) = @_;
      
      $self->{_data}->{indexes_seq} = [];
      my $explained = $self->explain->explain($self->sql_builder->sql);
      
      my %indexes = ();
      foreach my $explain (@{$explained->results}) {
            push @{$indexes{$explain->{table}}} , split /,/ , $explain->{possible_keys}
                  if $explain->{possible_keys};
      }
      
      my $indo = MySQL::Query::IndexesOrder->new();
      my $comb = $indo->indexes_comb(\%indexes , $self->tables);
      
      foreach my $keys_list (@$comb) {
            my $cloned = dclone($self->sql_builder);
            foreach my $entry (@$keys_list) {
                  my $table = (keys %$entry)[0];
                  $self->hints->add_use_index($cloned , $table , $entry->{$table});
            }
            
            my $sql = $cloned->sql;
            #print $sql;
            my $exp = $self->explain->explain($sql);
            
            my $t0 = [gettimeofday];
            $self->dbh->do($sql);
            my $bench_time = tv_interval ( $t0, [gettimeofday]);
            
            my %temp = ();
            $temp{indexes_seq_sql} = $sql;
            $temp{indexes_seq_bench} = $bench_time;
            $temp{indexes_seq_explain} = $self->explain->results;
            $temp{indexes_seq_totalrows} += $self->explain->total_rows;
            
            push @{$self->{_data}->{indexes_seq}} , \%temp;
      }
}

sub as_html {
      my ($self , $data) = @_;
        
      my $tmpl = HTML::Template->new( filename => "../share/tmpl/indexes.html" , die_on_bad_params => 0 );
      
      $tmpl->param(indexes => $self->{_data}->{indexes});
      $tmpl->param(seq => $self->{_data}->{indexes_seq});
      $tmpl->param(nohighlight => 1) if $self->{nohighlight};
      
      my $path = $self->{output};
      
      open (FH , "+>$path/analyze_indexes.html") || die print $!;
            print FH $tmpl->output;
      close FH;
        
      print "Indexes report generated in html file: $path/analyze_indexes.html\n"; 
}

sub as_text {
      print "Text report is not implemented yet";
}

1;
